/* eslint-disable */
import {
  login,
  loginout
} from '@/api/user';
import {
  constRoutes,
  routes
} from '@/router/routes';
import {
  filterAsyncRouter
} from '@/utils/util';
const roleMenuMap = {
  '医护人员': [
    // {
    //   "children": [{
    //     "children": [],
    //     "menuId": "3ae6594a32e7b36c18486a1b36286b2c",
    //     "parentId": "85700bfcf6a984b6901200a21a588c4c",
    //     "path": "/pets/info",
    //     "title": "宠物信息"
    //   }],
    //   "icon": "s-shop",
    //   "menuId": "85700bfcf6a984b6901200a21a588c4c",
    //   "parentId": "0",
    //   "path": "/pets",
    //   "sort": 3,
    //   "title": "宠物管理"
    // },
    {
      "children": [],
      "icon": "s-home",
      "menuId": "8a30e6ac55f653657c3aaee772bbbb15",
      "parentId": "0",
      "path": "/welcome",
      "sort": 1,
      "title": "首页"
    },
    {
      "children": [{
          "children": [],
          "menuId": "28cb260f39b9cd5d4eea3954cf74e3af",
          "parentId": "b706ec97ceb6280517a0bff1ea75bf61",
          "path": "/appoint/list",
          "title": "预约列表"
        },
        {
          "children": [],
          "menuId": "67889a32bca8633efdd341ca587a7092",
          "parentId": "b706ec97ceb6280517a0bff1ea75bf61",
          "path": "/appoint/communicate",
          "title": "在线沟通"
        },
        {
          "children": [],
          "menuId": "f02fcdfd3521f904d9aac801c3b1715f",
          "parentId": "b706ec97ceb6280517a0bff1ea75bf61",
          "path": "/appoint/guide",
          "title": "健康指南"
        }
      ],
      "icon": "s-claim",
      "menuId": "b706ec97ceb6280517a0bff1ea75bf61",
      "parentId": "0",
      "path": "/appoint",
      "sort": 4,
      "title": "预约管理"
    },
    {
      "children": [
        // {
        //   "children": [],
        //   "menuId": "78c24642b052716d6109e3528d67b8a5",
        //   "parentId": "ff36314f95dd74d4c70f0b0cb3fcbcd1",
        //   "path": "/hospital/staff",
        //   "title": "人员信息"
        // },
        // {
        //   "children": [],
        //   "menuId": "a750be41a872a986ad0688b8e850c0fc",
        //   "parentId": "ff36314f95dd74d4c70f0b0cb3fcbcd1",
        //   "path": "/hospital/goods",
        //   "title": "物品"
        // },
        {
          "children": [],
          "menuId": "18b0c1f9ea10fff51e496b3d9edc9eb4",
          "parentId": "ff36314f95dd74d4c70f0b0cb3fcbcd1",
          "path": "/hospital/working",
          "title": "排班"
        }
      ],
      "icon": "s-management",
      "menuId": "ff36314f95dd74d4c70f0b0cb3fcbcd1",
      "parentId": "0",
      "path": "/hospital",
      "sort": 2,
      "title": "医院管理"
    }
  ]
}
const user = {
  state: {
    token: sessionStorage.getItem('token'),
    userInfo: JSON.parse(sessionStorage.getItem('userInfo') || '{}'),
    menuList: [...constRoutes, ...filterAsyncRouter(JSON.parse(sessionStorage.getItem('menuList') || '[]'))],
    // menuList: [...constRoutes, ...routes],
    roleList: JSON.parse(sessionStorage.getItem('roleList') || '[]')
  },
  mutations: {
    SetMenuList(state, value) {
      let arr = [...constRoutes];
      if (value && value.length > 0) {
        arr.push(...filterAsyncRouter(value));
      }
      state.menuList = arr && arr.sort((a, b) => a.sort - b.sort);
      // state.menuList = [...constRoutes, ...routes];
    },
    LogOut(state) {
      state.token = '';
      state.userInfo = {};
      state.menuList = [...constRoutes];
      state.roleList = [];
      sessionStorage.clear();
    }
  },
  actions: {
    Login({
      commit,
      state
    }, loginForm) {
      return new Promise((resolve, reject) => {
        login(loginForm)
          .then((res) => {
            state.userInfo = res.data;
            state.token = res.token;
            state.roleList = res.data.roleList;
            let role = res.data.roleList && res.data.roleList[0].role;
            let menuList = roleMenuMap[role] || res.data.menuList || []
            commit('SetMenuList', [...menuList]);
            sessionStorage.setItem('token', JSON.stringify(res.token));
            sessionStorage.setItem('userInfo', JSON.stringify({
              ...res.data
            }));
            sessionStorage.setItem('roleList', JSON.stringify([...res.data.roleList]));
            sessionStorage.setItem('menuList', JSON.stringify([...res.data.menuList]));
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    LoginOut({
      commit,
      state
    }) {
      return new Promise((resolve, reject) => {
        loginout(state.token)
          .then(() => {
            commit('LogOut');
            resolve();
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // getSelectData({commit, state}) {
    //   return new Promise((resolve, reject) => {
    //     // 对接接口
    //     refreshToken(state.token).then(res => {
    //       // 添加数据
    //       commit('SET_EXPIRES_IN', res.data)
    //       resolve()
    //     }).catch(error => {
    //       reject(error)
    //     })
    //   })
    // },
  },
};

export default user;